package cn.jxau.fukin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jxau.fukin.model.Diary;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.service.IDiaryService;
import cn.jxau.fukin.service.IUserService;

import com.google.gson.Gson;

/**
 * Description:登录和注册
 * Title:UserController
 * @author ChunFu-Lee
 * @date 2017-9-9 下午8:27:57
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IDiaryService diaryService;

	@RequestMapping(value = "/showLoginPage.do")
	public String showLoginPage() {
		return "redirect:/login.jsp";
	}

	@RequestMapping(value = "/showRegisterPage.do")
	public String showRegisterPage() {
		return "redirect:/register.jsp";
	}

	/**
	 * 登录
	 * @param user
	 * @param mv
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login.do")
	public ModelAndView login(User user, ModelAndView mv, HttpSession session,
			HttpServletRequest request) throws Exception {
		List<User> userinfo = userService.queryUserInfo(user);
		String code = request.getParameter("code");
		String vcode = (String) request.getSession().getAttribute("verifycode");
		System.out.println(code + ":" + vcode);
		request.getSession().setAttribute("user", user);
		if (!code.equalsIgnoreCase(vcode)) {
			session.setAttribute("codeError", "验证码错误");
			mv.setViewName("redirect:/login.jsp");
			return mv;
		}
		if (userinfo != null && !userinfo.isEmpty()) {
			User u = userinfo.get(0);
			// System.out.println(u);
			session.setAttribute("flag", "success");
			mv.setViewName("redirect:/index.jsp");
			session.setAttribute("user", u);
		} else {
			session.setAttribute("codeError", "");
			session.setAttribute("msg", "登录失败");
			mv.setViewName("redirect:/login.jsp");
		}
		// System.out.println(userinfo);
		return mv;
	}

	/**
	 * 注册
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/register.do", produces = "text/html;charset=UTF-8")
	public String addUserInfo(User user, HttpServletRequest request) {
		String addUserInfo = userService.addUserInfo(user);
		if ("success".equals(addUserInfo)) {
			request.getSession().setAttribute("msg", "注册成功");
			return "redirect:/login.jsp";
		}
		request.getSession().setAttribute("error", "注册失败");
		return "redirect:/register.jsp";
	}

	@ResponseBody
	@RequestMapping(value = "/selectAllDiarys.do", produces = "text/html;charset=UTF-8")
	public String selectAllDiarys() {
		Gson gson = new Gson();
		return gson.toJson(diaryService.selectAllDiarys());
	}

	@ResponseBody
	@RequestMapping(value = "/addDiary.do", produces = "text/html;charset=UTF-8")
	public void addDiary(Diary diary, HttpSession session) {
		User user = (User) session.getAttribute("user");
		diary.setdUId(user.getuId());
		diaryService.insertDiary(diary);
	}

}
